$(function () {
    syb('#affiliationSyb');
    countStaleTotal();
    countAptitude();
    loadWarningAccount('[name="warningAccount"]');
});

/**
 *
 */
function countAptitude(signOrgId) {
    var params = {};
    if (!checkIsEmpty(signOrgId)) {
        params.signOrgId = signOrgId;
    }
    var checkResult = checkHasSybAuth($('#affiliationSyb option'));
    if (!checkResult) {
        params.isAll = "0"
    }
    $.post(sysInfo.basePath + "/zs/credential/pandect/get_count_staleTotal.do", params, function (result) {
        if (result.code == 0) {
            //录入资质量
            $("#countTodaysEntry").html(checkIsEmpty(result.data.countTodaysEntry) ? 0 : result.data.countTodaysEntry);
            //资质总量
            $("#countEntryTotal").html(checkIsEmpty(result.data.countEntryTotal) ? 0 : result.data.countEntryTotal);
            //资质预定量
            $("#countBookings").html(checkIsEmpty(result.data.countBookings) ? 0 : result.data.countBookings);
            //全国适用资质量
            $("#countNativeTotal").html(checkIsEmpty(result.data.countNativeTotal) ? 0 : result.data.countNativeTotal);
            //本地适用资质量
            $("#countNationwideTotal").html(checkIsEmpty(result.data.countNationwideTotal) ? 0 : result.data.countNationwideTotal);
        } else {
            DGG.Errors(result.msg);
        }
    })
}

//过期总量
function countStaleTotal(signOrgId) {
    var checkResult = checkHasSybAuth($('#affiliationSyb option'));
    var isAll = "";
    if (!checkResult) {
        isAll = "0"
    }
    $.post(sysInfo.basePath + "/zs/credential/qualification/get_stale_total.do", {signOrgId: signOrgId, isAll: isAll}, function (re) {
        if (re.code == 0) {
            $("#countStaleTotal").html(re.data);
        } else {
            DGG.Errors(re.msg);
        }
    });
}


//搜索
function searchAptitude() {
    var signOrgId = $("#affiliationSyb").val();
    countAptitude(signOrgId);
    countStaleTotal(signOrgId);
    loadWarningAccount('[name="warningAccount"]', signOrgId);
}

/**
 * 跳转详情
 * @param type 类型
 * @param val 值
 */
function goToDentails(type, val) {
    switch (type) {
        case 1: // 录入量
            window.parent.updateTab("资质信息列表", sysInfo.basePath + "/zs/credential/qualification/index.html?systemDate=" + val);
            break;
        case 2: // 总量
            window.parent.updateTab("资质信息列表", sysInfo.basePath + "/zs/credential/qualification/index.html");
            break;
        case 3: // 预定量
            window.parent.updateTab("资质信息列表", sysInfo.basePath + "/zs/credential/qualification/index.html?reserve=" + val);
            break;
        case 4: // 过期总量
            window.parent.updateTab("资质过期预警", sysInfo.basePath + "/zs/warning/aptitude/stale_index.html?overdueStatus=" + val);
            break;
        case 5: // 全国量
            window.parent.updateTab("资质信息列表", sysInfo.basePath + "/zs/credential/qualification/index.html?aScopes=" + val);
            break;
        case 6: //本地量
            window.parent.updateTab("资质信息列表", sysInfo.basePath + "/zs/credential/qualification/index.html?aScopes=" + val);
            break;
    }
}


/**
 * 加载预订信息
 */
function loadWarningAccount(obj, signOrgId) {
    var params = {};
    var checkResult = checkHasSybAuth($('#affiliationSyb option'));
    if (!checkResult) {
        params.isAll = "0"
    }
    if (!checkIsEmpty(signOrgId)) {
        params.signOrgId = signOrgId;
    }
    $.post(sysInfo.basePath + "/zs/credential/qualification/wanrning_count.do", params, function (result) {
        if (result.code == 0) {
            var datas = result.data;
            $(obj).empty();
            $(obj).append("<thead>\n" +
                "<tr>\n" +
                "    <th>资质类型</th>\n" +
                "    <th>在库可用量</th>\n" +
                "    <th>匹配量</th>\n" +
                "    <th>预定量</th>\n" +
                "    <th>预约量</th>\n" +
                "</tr>\n" +
                "</thead><tbody>");
            if (!checkIsEmpty(datas) && datas.length > 0) {
                $.each(result.data, function (i, item) {
                    $(obj).append("<tr>\n" +
                        "<td>" + item.certificateMajorName + "</td>\n" +
                        "<td>" + item.libraryNumbers + "</td>\n" +
                        "<td>" + item.matchNumbers + "</td>\n" +
                        "<td>" + item.reserveNumbers + "</td>\n" +
                        "<td>" + item.orderNumbers + "</td>\n" +
                        "</tr>");
                });
                $(obj).append("</tbody>");
            } else {
                $(obj).append("<tr>\n" +
                    "<td colspan='5'><center>没有数据</center></td>\n" +
                    "</tr></tbody>");
            }

        } else {
            DGG.Errors(result.msg);
            return false;
        }
    });
}

function goToWarningPage() {
    window.parent.updateTab("资质库容预警", sysInfo.basePath + "/zs/warning/aptitude/storage_index.html");
}
